
/** \file   common_i2c.c
 *
 *  \brief  This file contains I2C driver wrappers implemented for Applications
 *
 *   This file performs tests on the following:
 *
 *      1. Read/Write on I2C bus.(From known address)
 *
 *
 *   Note: N/A
 *
 *  \author     Jiawei Xie
 *
 *  \version    0.1   Created newly for NAVCON
 */

/* ========================================================================== */
/*                          INCLUDE FILES                                     */
/* ========================================================================== */

#include <std.h>
#include <stdio.h>
#include <string.h>
#include <log.h>
#include <tsk.h>
#include <ecm.h>
#include <gio.h>
#include "ti/pspiom/i2c/I2c.h"


#include "common.h"
#include "stdio.h"
#include "math.h"
#include "common_i2c.h"

/* ========================================================================== */
/*                         IMPORTED VARIABLES                                 */
/* ========================================================================== */


/* ========================================================================== */
/*                            MACRO DEFINTIONS                                */
/* ========================================================================== */



/* ========================================================================== */
/*                            GLOBAL VARIABLES                                */
/* ========================================================================== */



/* ========================================================================== */
/*                          Global FUNCTION PROTOTYPE                          */
/* ========================================================================== */
/* Global function prototypes */
void user_i2c_init(void);
/**
 *  brief i2cRead function cannot be called from a SWI, HWI, or main() unless the
 *  underlying mini-driver is a non-blocking driver and the GIO Manager
 *  properties are set to use non-blocking synchronization methods.
 */
int i2cRead(GIO_Handle inHandle, unsigned int deviceId,  unsigned char *buf,
		     unsigned int len, unsigned int flag)
{
    I2c_DataParam        dataBuffer;
    int status = IOM_COMPLETED;

	bzeros(buf,len); //clear the buffer first

    dataBuffer.slaveAddr = deviceId;
    dataBuffer.buffer    = buf;
    dataBuffer.bufLen    = len; /* 2 bytes of address */
    dataBuffer.flags     = flag; //I2c_WRITE | I2c_MASTER | I2c_START;
    /*
     * Synchronous read command.
     * GIO_read returns IOM_COMPLETED upon successfully reading the requested
     * number of MADUs from the device. If an error occurs, the device returns
     * a negative value. Calling GIO_read can result in the thread blocking.
     *
     */
    status = GIO_read(inHandle, &dataBuffer,&dataBuffer.bufLen);
    return status;
}

/**
 *  brief i2cWrite function cannot be called from a SWI, HWI, or main() unless the
 *  underlying mini-driver is a non-blocking driver and the GIO Manager
 *  properties are set to use non-blocking synchronization methods.
 */
int i2cWrite(GIO_Handle outHandle, unsigned int deviceId,  unsigned char *buf,
		     unsigned int len, unsigned int flag)
{
    I2c_DataParam        dataBuffer;
    int status = IOM_COMPLETED;

    dataBuffer.slaveAddr = deviceId;
    dataBuffer.buffer    = buf;
    dataBuffer.bufLen    = len; /* 2 bytes of address */
    dataBuffer.flags     = flag; //I2c_WRITE | I2c_MASTER | I2c_START;
    /*
     * Synchronous write command.
     * GIO_write returns IOM_COMPLETED upon successfully writing the
     * requested number of MADUs to the device. If an error occurs, the device
     * returns a negative value.
     *
     */
    status = GIO_write(outHandle, &dataBuffer,&dataBuffer.bufLen);

    return status;

}

/* ========================================================================== */
/*                                END OF FILE                                 */
/* ========================================================================== */
